package models;

import java.io.ByteArrayInputStream;
import java.util.Date;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.internet.MimeMessage;

import siena.Column;
import siena.Id;
import siena.Index;
import siena.Model;
import siena.Query;

/**
 * MMS Mobile Originated
 * 
 * @author Aldrin Leal
 * 
 */
public class MMSMO extends Model {
	@Id
	public Long oid;

	@Column("from")
	@Index("from_index")
	String from;

	@Column("dateTime")
	Date dateTime;

	@Column("to")
	@Index("to_index")
	String toAddress;

	@Column("content")
	String content;

	@Column("usuarioId")
	String usuarioId = null;

	@Column("dataPublicacao")
	Date dataPublicacao = null;

	String subject;

	public String getFrom() {
		return from;
	}

	public void setFrom(String originator) {
		this.from = originator;
	}

	public Date getDateTime() {
		return dateTime;
	}

	public void setDateTime(Date dateTime) {
		this.dateTime = dateTime;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getUsuarioId() {
		return usuarioId;
	}

	public void setUsuarioId(String usuarioId) {
		this.usuarioId = usuarioId;
	}

	public Date getDataPublicacao() {
		return dataPublicacao;
	}

	public void setDataPublicacao(Date dataPublicacao) {
		this.dataPublicacao = dataPublicacao;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getToAddress() {
		return toAddress;
	}

	public void setToAddress(String to) {
		this.toAddress = to;
	}

	public MimeMessage asMimeMessage() throws Exception {
		MimeMessage mimeMessage = new MimeMessage(Session
				.getDefaultInstance(new Properties()),
				new ByteArrayInputStream(getContent().getBytes("ASCII")));

		return mimeMessage;
	}

	public static Query<MMSMO> all() {
		return Model.all(MMSMO.class);
	}

	public static Query<MMSMO> outstandingMessages() {
		Query<MMSMO> outstandingQuery = Model.all(MMSMO.class);

		outstandingQuery.filter("dataPublicacao", null);

		return outstandingQuery;
	}
}
